/**
 * Created by lixiang01 on 2/16/2017.
 */

$(function () {

    var getQueryString = function(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return r[2]; return "";
    };
    var dataId = getQueryString("id");

    var saleData ={
        saleInfo:{
            productSysNo : "",
            color: "",
            size: "",
            salePrice:"",
            sex:"1",
            saleNum:"1",
            mobilePhone:""
        }

    };

    Vue.http.get(webRoot + "/sale/getSaleById/" + dataId, {credentials: true}).then(function (response) {
            var datas = response.body;
            if (datas['error'] == 0) {
                saleData.saleInfo = datas['object'][0];

            } else {
                alert(datas['message'])
            }

        },
        function (response) {
            // error callback
        });



    var saleEditVue = new Vue({
        el:'#divController',
        data:function () {
            return saleData;
        },
        methods:{
            saveSale:function () {


                this.$http.post(webRoot+'/sale/updateSale/', this.saleInfo,  {credentials: true}).then(function(response)
                    {
                        var datas = response.body;
                        if(datas['error']=='0'){
                            window.location.reload();
                        }else{
                            alert(datas['message']);
                        }

                    },
                    function(response){
                        // error callback
                    });

            },
            deleteSale:function (event) {


                var btnArray = ['否', '是'];
                mui.confirm('确认删除这条记录？', '删除', btnArray, function(e) {
                    if (e.index == 1) {
                        Vue.http.get(webRoot+'/sale/deleteSaleById/'+dataId,  {credentials: true}).then(function(response)
                            {
                                var datas = response.body;
                                if(datas['error']=='0'){
                                    window.location.href="report.html"
                                }else{
                                    alert(datas['message']);
                                }

                            },
                            function(response){
                                // error callback
                            });
                    } else {

                    }
                })



            }

        }

    });


});




